﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UCF.Core.Forms.CustomControls.SoSearchPanel
{
    public partial class frmSearchPanel : Form
    {

        /// <summary>
        /// 获取或设置搜索方法
        /// </summary>
        public Action<string> SearchHandler { get; set; }

        /// <summary>
        /// 是否启用调试模式
        /// </summary>
        public bool IsDebug { get; set; }

        /// <summary>
        /// 获取或设置搜索字段
        /// <para>ID | Name | Type | Default | DataSource</para>
        /// </summary>
        public DataTable Fields { get; set; }

        public frmSearchPanel()
        {
            InitializeComponent();
        }

        private void frmSearchPanel_Load(object sender, EventArgs e)
        {

            Label lblTitle;
            Control ctrlInput;
            string[] arr;
            Panel panelGroup;
            int h = 30, left;
            string title;
            foreach (DataRow r in Fields.Rows)
            {
                title = r["Title"].ToString();
                left = title.Length * 12 + 30;
                lblTitle = new Label() { Text = title, Width = left - 30, Left = 10, Top = h };
                this.Controls.Add(lblTitle);
                switch (r["Type"].ToString())
                {
                    case "textbox":
                        ctrlInput = new TextBox() { Left = left, Top = h };
                        this.Controls.Add(ctrlInput);
                        break;
                    case "datepicker":
                        ctrlInput = new DateTimePicker() { Left = left, Top = h };
                        this.Controls.Add(ctrlInput);
                        break;
                    case "checkbox":
                        ctrlInput = new CheckBox() { Left = left, Top = h };
                        this.Controls.Add(ctrlInput);
                        break;
                    case "checkbox_group":
                        panelGroup = new Panel() { Left = left, Top = h };
                        foreach (string kv in r["data"].ToString().Split('&'))
                        {
                            arr = kv.Split('=');
                            ctrlInput = new CheckBox() { Text = arr[0], Tag = arr[1], Left = left + 60, Top = 0 };
                            panelGroup.Controls.Add(ctrlInput);
                        }
                        this.Controls.Add(panelGroup);
                        break;
                    case "radio":
                        ctrlInput = new RadioButton() { Left = left, Top = h };
                        this.Controls.Add(ctrlInput);
                        break;
                    case "radio_group":
                        panelGroup = new Panel() { Left = left, Top = h };
                        foreach (string kv in r["data"].ToString().Split('&'))
                        {
                            arr = kv.Split('=');
                            ctrlInput = new RadioButton() { Text = arr[0], Tag = arr[1], Left = left + 60, Top = 0 };
                            panelGroup.Controls.Add(ctrlInput);
                        }
                        this.Controls.Add(panelGroup);
                        break;
                    case "combobox":
                        ctrlInput = new ComboBox() { Left = left, Top = h };
                        this.Controls.Add(ctrlInput);
                        break;
                    case "combobox_multi":

                        break;
                    default:
                        ctrlInput = new Label() { Text = "无法识别的数据类型", Left = left, Top = h };
                        this.Controls.Add(ctrlInput);
                        break;
                }
                h += 30;
            }
        }
    }
}
